package com.tianyang.yukuaifu.business.trade.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.trade.adapter.MerchantAchieveAdapter;
import com.tianyang.yukuaifu.business.trade.bean.MerchantAchieveOutput;
import com.tianyang.yukuaifu.business.trade.contract.MerchantAchieveContract;
import com.tianyang.yukuaifu.business.trade.contract.TradeConfig;
import com.tianyang.yukuaifu.business.trade.presenter.MerchantAchievePresenter;
import com.tianyang.yukuaifu.business.trade.provider.TradeOpenProvider;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.common.utils.DateCheckPopListener;
import com.tianyang.yukuaifu.common.view.SearchCustomView;
import com.tianyang.yukuaifu.module.dialog.AchievementDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.IAchieveListener;
import com.tianyang.yukuaifu.module.roundview.RoundTextView;
import com.tianyang.yukuaifu.module.wheel.interfaces.IWheelDataChangeCallback;
import com.tianyang.yukuaifu.module.widget.VerticalSwipeRefreshLayout;
import com.tianyang.yukuaifu.utils.DateUtils;
import com.tianyang.yukuaifu.utils.SoftInputUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：商户业绩
 * Created by xyj on 2024/3/25
 */
public class MerchantAchieveActivity extends BaseTitleActivity implements MerchantAchieveContract.View {

    @BindView(R.id.searchCv)
    SearchCustomView searchCv;
    @BindView(R.id.phoneNumberEt)
    EditText phoneNumberEt;
    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView vRecyclerview;
    @BindView(R.id.emptyView)
    RelativeLayout emptyView;

    @BindView(R.id.filterTv)
    TextView filterTv;
    @BindView(R.id.dayFilterRtv)
    RoundTextView dayFilterRtv;
    @BindView(R.id.monthFilterRtv)
    RoundTextView monthFilterRtv;

    private AchievementDialog vAchievementDialog;

    private DateCheckPopListener mDatePopListener;

    private MerchantAchieveAdapter vMerchantAchieveAdapter;

    private MerchantAchieveContract.Presenter mPresenter;

    private int nowPage = CommonConfig.PAGE_NUM;

    private boolean isMonthFilter = false;

    private String startTime, endTime;

    private boolean isSubMerchant;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_merchant_achieve;
    }

    @Override
    protected void initView() {
        super.initView();
        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        isSubMerchant = getIntent().getBooleanExtra(TradeConfig.INTENT_IS_SUB_MERCHANT, false);
        int layout = R.layout.item_merchant_achieve;
        if (isSubMerchant) {
            layout = R.layout.item_sub_merchant_achieve;
            commonTitle.setTitle("下级服务商业绩");
        } else {
            commonTitle.setTitle("我的商户业绩");
            searchCv.setVisibility(View.VISIBLE);
        }
        vMerchantAchieveAdapter = new MerchantAchieveAdapter(layout, isSubMerchant);
        vRecyclerview.setAdapter(vMerchantAchieveAdapter);
        vMerchantAchieveAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);
        vMerchantAchieveAdapter.setPreLoadNumber(2);

        mPresenter = new MerchantAchievePresenter(this, isSubMerchant);
        setTimeData(DateUtils.getTimeAgo(30, "yyyy-MM-dd"), DateUtils.getCurrentTime("yyyy-MM-dd"));
    }

    @OnClick({R.id.filterTv, R.id.dayFilterRtv, R.id.monthFilterRtv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.filterTv:
                if (isMonthFilter) {
                    if (mDatePopListener == null) {
                        mDatePopListener = new DateCheckPopListener((Activity) mContext, true);
                    }
                    mDatePopListener.handleRegionCheck(view, "请选择月份", false, new IWheelDataChangeCallback() {
                        @Override
                        public void wheelDataChangeCallback(String wheelData) {
                            setTimeData(wheelData, wheelData);
                        }
                    });
                } else {
                    if (vAchievementDialog == null) {
                        vAchievementDialog = new AchievementDialog(mContext);
                        vAchievementDialog.setListener(new IAchieveListener() {
                            @Override
                            public void callback(String startTime, String endTime) {
                                setTimeData(startTime, endTime);
                            }
                        });
                    }
                    vAchievementDialog.setTimeData(startTime, endTime);
                    vAchievementDialog.show();
                }
                break;
            case R.id.dayFilterRtv:
                if (isMonthFilter) {
                    isMonthFilter = false;
                    showDayOrMonthView();
                }
                break;
            case R.id.monthFilterRtv:
                if (!isMonthFilter) {
                    isMonthFilter = true;
                    showDayOrMonthView();
                }
                break;
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        phoneNumberEt.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    SoftInputUtil.hideSoftInput(MerchantAchieveActivity.this);
                    nowPage = CommonConfig.PAGE_NUM;
                    mPresenter.posMerchantTradeTotal(nowPage, startTime, endTime, phoneNumberEt.getText().toString());
                    vMerchantAchieveAdapter.setEnableLoadMore(true);
                }
                return false;
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = CommonConfig.PAGE_NUM;
                mPresenter.posMerchantTradeTotal(nowPage, startTime, endTime, phoneNumberEt.getText().toString());
                vMerchantAchieveAdapter.setEnableLoadMore(true);
            }
        });
        vMerchantAchieveAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.posMerchantTradeTotal(nowPage, startTime, endTime, phoneNumberEt.getText().toString());
            }
        }, vRecyclerview);
        vMerchantAchieveAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MerchantAchieveOutput output = ((MerchantAchieveAdapter) adapter).getItem(position);
                if (output != null) {
                    if (isSubMerchant) {
                        TradeOpenProvider.getInstance().openSubAgentStatActivity(
                                mContext, output.getAgentName(), output.getAgentPhone(), startTime, endTime, isMonthFilter);
                    } else {
                        TradeOpenProvider.getInstance().openSubMerchantStatActivity(
                                mContext, output.getName(), output.getPhone(), startTime, endTime, isMonthFilter);
                    }
                }
            }
        });
    }

    private void showDayOrMonthView() {
        if (isMonthFilter) {
            dayFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
            dayFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
            monthFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            monthFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
            setTimeData(DateUtils.getCurrentMonth(), DateUtils.getCurrentMonth());
        } else {
            monthFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
            monthFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
            dayFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            dayFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
            setTimeData(DateUtils.getTimeAgo(30, "yyyy-MM-dd"), DateUtils.getCurrentTime("yyyy-MM-dd"));
        }
    }

    @Override
    public void showMerchantTradeView(List<MerchantAchieveOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size() > 0) {
            if (isFirst) {
                vMerchantAchieveAdapter.setNewData(outputs);
            } else {
                vMerchantAchieveAdapter.addData(outputs);
            }
            nowPage++;
            vRecyclerview.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
        } else if (isFirst) {
            vRecyclerview.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                vMerchantAchieveAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                vMerchantAchieveAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                vMerchantAchieveAdapter.loadMoreFail();
                break;
        }
    }

    @SuppressLint("SetTextI18n")
    private void setTimeData(String startTimeStr, String endTimeStr) {
        startTime = startTimeStr;
        endTime = endTimeStr;
        if (isMonthFilter) {
            filterTv.setText(startTime.replaceAll("-", "/"));
        } else {
            filterTv.setText(startTime.replaceAll("-", "/") + "-" + endTime.replaceAll("-", "/"));
        }
        nowPage = CommonConfig.PAGE_NUM;
        mPresenter.posMerchantTradeTotal(nowPage, startTime, endTime, phoneNumberEt.getText().toString());
        vMerchantAchieveAdapter.setEnableLoadMore(true);
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MerchantAchieveContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
